package com.socialrss.viewlisteners;
/*
* author : Sefa Sahin Koc
*/

import com.socialrss.R;
import com.socialrss.accounts.Group;
import com.socialrss.utils.SocialRssModel;

import android.os.Bundle;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.WindowManager.LayoutParams;

import android.view.View;
import android.view.View.OnClickListener;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.PopupWindow;
import android.widget.TableRow;
import android.widget.TextView;

public class GroupConnections extends BaseActivity implements OnClickListener {
	private Group thiscat;
	private TextView cateNameTextView, numOfContentsTextView;
	private CheckBox enable;
	private boolean firstValue;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);		
		setContentView(R.layout.activity_group_connections);
		View addMoreConnButton =findViewById(R.id.attachMorePeopleButton);
		View editPropButton = findViewById(R.id.editCategoryPropertiesButton);
		addMoreConnButton.setOnClickListener(this);
		editPropButton.setOnClickListener(this);
		
		//get selected category
		int catId = getIntent().getExtras().getInt("categoryid");
		thiscat = (Group)SocialRssModel.Instance(this).allCategories().get(catId);
		
		cateNameTextView = (TextView)findViewById(R.id.categoryNameTextView);
		cateNameTextView.setText(thiscat.getName());
		//set text views		
		TableRow tabler = (TableRow)findViewById(R.id.tableRow1);
		numOfContentsTextView = (TextView)tabler.findViewById(R.id.numOfContentsTextView2);
		numOfContentsTextView.setText(String.valueOf(thiscat.getNumberOfElements()));
		
		//set checkbox
		tabler = (TableRow)findViewById(R.id.tableRow2);
		enable = (CheckBox)tabler.findViewById(R.id.groupNotificationCheckBox);
		enable.setChecked(thiscat.isTurnedOn());
		firstValue = thiscat.isTurnedOn();
		this.setCheckboxListener();
	}/*
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_group_connections, menu);
		return true;
	}*/
	/**
	 * Button listeners are specified
	 */
	public void onClick(View v) {
		switch (v.getId()) {
			case R.id.attachMorePeopleButton:
				if(firstValue != thiscat.isTurnedOn()){
					SocialRssModel.Instance(null).groupTurnedOnIsUpdated(thiscat.getId());
				}
				Intent i = new Intent(this, AddMoreConnections.class);
				Bundle b = new Bundle();
				b.putIntArray("idsOfParents", new int[]{});
				b.putInt("categoryid", thiscat.getId());
				i.putExtras(b);
				startActivity(i);
				break;
			case R.id.editCategoryPropertiesButton:
				LayoutInflater layoutInflater = (LayoutInflater)getBaseContext().getSystemService(LAYOUT_INFLATER_SERVICE);
				View popupView = layoutInflater.inflate(R.layout.edit_text_popup, null);
				final PopupWindow editpopup = new PopupWindow(popupView,LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
				//specify cancel button
				View cancelbutton = popupView.findViewById(R.id.popupcancelbutton);
				cancelbutton.setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View arg0) {
						editpopup.dismiss();
					}
				});
				//specify edittext
				final EditText catnameedittext = (EditText)popupView.findViewById(R.id.editTextInPopup);
				catnameedittext.setText(thiscat.getName());
				catnameedittext.setHint("Category Name");
				
				//specify ok button
				View okbutton = popupView.findViewById(R.id.popupcommitbutton);
				okbutton.setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View arg0) {
						String newname = catnameedittext.getText().toString();
						thiscat.setName(newname);
						cateNameTextView.setText(newname);
						SocialRssModel.Instance(null).groupNameIsUpdated(thiscat.getId(), newname);
						editpopup.dismiss();
					}
				});
				
				editpopup.setFocusable(true);
				editpopup.showAsDropDown(findViewById(R.id.tableRow1), 0, -10);
				break;
				
			// More buttons go here (if any) ...
		}
	}
	private void setCheckboxListener(){
		enable.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				GroupConnections.this.thiscat.setTurnedOn(isChecked);
			}
		});
	}
}
